<?php

namespace App\Listeners;

use App\Events\LoginLogEvent;
use App\Models\LoginLog;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class LoginLogListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  LoginLogEvent  $event
     * @return void
     */
    public function handle(LoginLogEvent $event)
    {
        //是否开启登陆日志开关
        if (setting('login_log') == 1){
            $user = $event->user;
            if ($user){
                //更新到用户表中的相应字段
                $user->last_login_ip = request()->getClientIp();
                $user->last_login_date = date('Y-m-d H:i:s');
                $user->save();
            }

            //记录到登陆日志表中
            (new LoginLog)->fill([
                'uid'=>$user ? $user->id : 0,
                'user_name'=>$user ? $user->name : request('email',''),
                'ip'=>request()->getClientIp(),
                'status'=>$event->status,
                'module'=>$event->module,
            ])->save();
        }

    }
}
